<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户字典</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
    <style>
        .layui-table .layui-input {
            text-align: center;
        }

        .editRowExt xm-select {
            min-height: 40px !important;
            line-height: 40px !important;
        }

        .editRowExt .layui-form-switch {
            top: 10px !important;
        }

        .query {
            display: none;
            padding: 0px 5px;
        }

        .fpdiv .layui-card-body {
            padding: 0px 10px;
        }

        .fpdiv .layui-card-body .layui-form {
            margin: 0px;
        }

    </style>
</head>
<body class="pear-container">
<div class="query">
    <form class="layui-form" action="">
        <div class="layui-row layui-col-space10">
            <div class="layui-col-md12">
                <div class="layui-card" style="margin-top: 10px;">
                    <div class="layui-card-header">用户名</div>
                    <div class="layui-card-body layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <input type="text" name="username" placeholder="账号" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-card" style="margin-top: 10px;">
                    <div class="layui-card-header">姓名</div>
                    <div class="layui-card-body layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <input type="text" name="nickname" placeholder="用户名" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-card" style="margin-top: 10px;">
                    <div class="layui-card-body layui-row layui-col-space10">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<div class="layui-row layui-col-space16 fpdiv">
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6 layui-col-lg6 layui-col-xl6">
        <div class="layui-card" style="height: 93vh">
            <div class="layui-card-header">用户列表（双击行选中）</div>
            <div class="layui-card-body userList">
                <table id="user-table" lay-filter="user-table"></table>
            </div>
        </div>
    </div>
    <div class="layui-col-xs6 layui-col-sm6 layui-col-md6 layui-col-lg6 layui-col-xl6">
        <div class="layui-card" style="height: 93vh">
            <div class="layui-card-header">选中的人员</div>
            <div class="layui-card-body">
                <div style="padding: 10px" class="layui-btn-container tag" lay-filter="user_sel_list"
                     lay-allowclose="true">

                </div>
            </div>
        </div>
    </div>
</div>
<div class="bottom">
    <div class="button-container">
        <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                lay-filter="save">
            <i class="layui-icon layui-icon-ok"></i>
            确定分配
        </button>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="query">
        <i class="icon pear-icon pear-icon-search"></i>
        查询
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="addAll">
        <i class="icon pear-icon pear-icon-add"></i>
        批量添加
    </button>
</script>

<script src="../../../config/apiUrl.js"></script>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['tag', 'newTable', 'form', 'jquery', 'common', 'http', 'toast', 'popup', 'drawer'], function () {
        let table = layui.newTable;
        let tag = layui.tag;
        let form = layui.form;
        let $ = layui.jquery;
        var http = layui.http;
        var toast = layui.toast;
        var popup = layui.popup;
        var drawer = layui.drawer;
        var common = layui.common;

        let MODULE_PATH = "/service-adminManage/organization/sysUser/";

        let cols = [
            [{
                type: 'numbers', fixed: "left"
            }, {
                type: 'checkbox', fixed: "left"
            },
                {
                    title: '账号',
                    field: 'username',
                    align: 'center',
                },
                {
                    title: '姓名',
                    field: 'nickname',
                    align: 'center',

                }
            ]
        ]

        var userTable = table.render({
            contentType: "application/x-www-form-urlencoded",
            height: 'full-95',
            elem: '#user-table',
            url: MODULE_PATH + 'getList',
            page: true,
            cols: cols,
            skin: 'grid',
            method: 'post',
            toolbar: '#user-toolbar',
            limit: 10,
            limits: [10,50,100],
            request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'limit' //每页数据量的参数名，默认：limit
            },
            headers: {
                "authorization": http.ajax.getToken()
            },
            defaultToolbar: ['filter', 'print', 'exports']
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.status, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.records //解析数据列表
                };
            }
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            },
            done: function (res, curr, count) {

            }
        });


        table.on('tool(user-table)', function (obj) {

        });

        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'query') {
                drawer.open({
                    direction: "right",
                    dom: ".query",
                    distance: "30%"
                });
            } else if (obj.event === 'addAll') {
                let data = table.checkStatus(obj.config.id).data;
                if(data.length == 0){
                    toast.error({title:false,message:"至少选中一个人员"})
                    return
                }
                for (let datum of data) {
                    tag.delete('user_sel_list', datum.id);
                    tag.add(
                        "user_sel_list",
                        {
                            text: datum.nickname + "[" + datum.username + "]"
                            ,
                            id: datum.id
                        }
                    );
                }
            }
        });
        table.on('rowDouble(user-table)', function (obj) {
            tag.delete('user_sel_list', obj.data.id);
            tag.add(
                "user_sel_list",
                {
                    text: obj.data.nickname + "[" + obj.data.username + "]"
                    ,
                    id: obj.data.id
                }
            );
        });
        form.on('submit(user-query)', function (data) {
            userTable = table.reload("user-table", {
                where: data.field
            }, true)
            return false;
        });
        form.on('submit(save)',async function (data) {
            let arr = tag.list('user_sel_list');
            if(arr.length == 0){
                toast.error({title:false,message:"至少选中一个人员"})
                return
            }
            let data_arr = [];
            let dep_id = common.getUrlParams("id")
            for (let arrElement of arr) {
                const {id,name} = arrElement;
                data_arr.push({
                    user_id:id,
                    dep_id:dep_id
                })
            }
            try {
                let res_data = await common.ajax({
                    url:apiUrlConfig.userMapping.userAllocation,
                    data:JSON.stringify(data_arr)
                })
                toast.success({title:false,message:"分配成功，并且清空了分配人员集合，可以继续分配"});
                for (let arrElement of arr) {
                    const {id,name} = arrElement;
                    tag.delete('user_sel_list', id);
                }
                parent.layui.table.reloadData('userList', {}, true)
            }catch (e){
                console.log(e)
            }
            return false;
        });
        tag.render("user_sel_list", {
            skin: 'layui-btn layui-btn-primary layui-btn-sm', //标签样式
        });
    })
</script>
</body>
</html>
